package com.dong.common.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class BIOServerTest {
    public static void main(String[] args) throws IOException {
        int port = 8080 ;
        //启动一个服务端，监听端口8080
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("server start at "+port);

        while(true){
            Socket socket = serverSocket.accept();
            //启动线程处理连接数据,如果不启动新线程处理数据，主线程就会阻塞在处理本次连接的数据
            new Thread(() ->{
                try {
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String msg ;
                    while ((msg = bufferedReader.readLine()) != null){
                        System.out.println("receive msg : "+msg);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
